package com.wang.head.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wang.common.controller.BaseController;
import com.wang.common.pojo.ApiResponse;
import com.wang.common.util.ConstatFinalUtil;
import com.wang.common.util.PageInfoUtil;
import com.wang.users.pojo.AComment;
import com.wang.users.pojo.ACommentEnum;
import com.wang.users.pojo.AUsers;
import com.wang.users.service.IGoodsDbService;
import com.wang.users.service.IUsersDbService;

/**
 * 评论表
 * @author lby
 *
 */
@Controller
@RequestMapping(value = "/head/goods",produces = "text/html;charset=UTF-8")
public class GoodsHeadController extends BaseController
{
	@Autowired
	private IGoodsDbService goodsDbService;
	
	@Autowired
	private IUsersDbService usersDbService;
	
	/**
	 * 评论列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/commentList")
	public String commentList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		AComment comment = (AComment) this.findObjfromSession(request, "comment");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		//condMap.put("usersId", comment.getId());
		condMap.put("status", ACommentEnum.STATUS_DISABLED.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AComment> apiResponse = this.goodsDbService.findCondListCommentService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/comment/commentList";
	}
	
	/**
	 * 打开添加管理员页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/commentInsert")
	public String commentInsert(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--commentInsert--");
		Map<String, Object> condMap = new HashMap<String, Object>();
		return "/head/comment/commentInsert" ;
	}
	

	/**
	 * 添加管理员提交操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "commentInsertSubmit")
	public String usersInsertSubmit(HttpServletRequest request,HttpServletResponse response,
			AComment comment,String email)throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--commentInsertSubmit--");
		
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("email", email);
		
		ApiResponse<AUsers> apiResponse = this.usersDbService.findOneUsersService(condMap);
		AUsers users = apiResponse.getDataOneJava();
		int usersId = users.getId();
		comment.setUsersId(usersId);
		
		/* 保存结果 */
		ApiResponse<Object> dbResponse = this.goodsDbService.saveOneCommentService(comment);
		ConstatFinalUtil.SYS_LOGGER.info("结果:{}", dbResponse.toJSON().toJSONString());
		
		request.setAttribute("response", dbResponse);
		return this.commentInsert(request, response);
	}
	
	

	/**
	 * 打开更新管理员页面
	 * 
	 * @return
	 * @throws IOException 
	 * @throws ServletException 
	 */
	@RequestMapping("/commentUpdate")
	public String commentUpdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
	{
		// operType=update&idType=session
		ConstatFinalUtil.SYS_LOGGER.info("--commentUpdate--");
		String id = request.getParameter("id");
		String operType = request.getParameter("operType");
		
		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<AComment> apiResponse = this.goodsDbService.findOneCommentService(condMap);
		/* 存储request */
		request.setAttribute("response", apiResponse.toJSON());
		request.setAttribute("operType", operType);
		
		if ("update".equalsIgnoreCase(operType))
		{
			return "/head/comment/commentUpdate";
		}
		return "/head/comment/commentInfo";
	}

	/**
	 * 更新管理员提交操作
	 * 
	 * @return
	 */
	@RequestMapping(value = "commentUpdateSubmit",produces = "text/html;charset=UTF-8" )
	@ResponseBody
	public String commentUpdateSubmit(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("--commentUpdateSubmit--");
		/* 数据库操作 */
		HttpSession session = request.getSession();
		ApiResponse dbResponse = new ApiResponse<>();
		String id = request.getParameter("id");
		String operType = request.getParameter("operType");

		/* 准备查询条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		/* 查询数据库 */
		ApiResponse<AComment> apiResponse = this.goodsDbService.findOneCommentService(condMap);
		/* 获取java对象 */
		AComment comment = apiResponse.getDataOneJava();
		boolean flag = false;
		if ("update".equalsIgnoreCase(operType))
		{
			/* 接收参数 */
//			String content = request.getParameter("content");
//			String fabulous = request.getParameter("fabulous");
//			String score = request.getParameter("score");
			String status = request.getParameter("status");
			
//			comment.setContent(content);
//			comment.setFabulous(Integer.parseInt(fabulous));
//			comment.setScore(Byte.valueOf(score));
			comment.setStatus(Byte.valueOf(status));
			flag = true;
		}
		dbResponse.setInfo(ConstatFinalUtil.INFO_JSON.getString(dbResponse.getCode() + ""), Collections.EMPTY_MAP);
		if (flag)
		{
			dbResponse = goodsDbService.updateOneCommentService(comment);
		}
		ConstatFinalUtil.SYS_LOGGER.info("--用户更新返回结果:{}--", dbResponse.toJSON());
		String result =  dbResponse.toJSON().toJSONString();
		return result;
	}
	
	
	
	
}
